<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <div class="shuxing">
        <el-form-item label="行政区划" prop="">
          <treeselect
            class="shuxing1"
            v-model="queryParams.areaId"
            :options="deptOptions"
            :normalizer="normalizer"
            placeholder="选择区域"
          />
        </el-form-item>
      </div>
     
      
   
      <el-form-item label="主体名称" prop="qualityCompany">
        <el-input
          v-model="queryParams.enterName"
          placeholder="请输入主体名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="qualityConclusion">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus"  size="mini"  @click="handleAdd" v-hasPermi="['productTestInfo:testInfot:add']">新增</el-button>
        
      </el-form-item>
    </el-form>

  

    <el-table v-loading="loading" :data="testInfotList" @selection-change="handleSelectionChange">
      <el-table-column
        label="序号"
        align="center"
        type="index"
        :index="indexMethod"
        width="80"
      />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主体名称" align="center" prop="enterName" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="质检结论" align="center" prop="qualityConclusion" />
      <el-table-column label="质检单位" align="center" prop="qualityCompany" />
      <el-table-column label="同步状态" align="center" prop="productTraceCode" />
      <!--  <el-form-item label="质检照片" prop="baseImg">
          <img :src="baseImg" class="news-image"/>
        </el-form-item> -->
         <el-table-column label="质检照片" align="center" prop="qualityImg" >
           <template slot-scope="scope">
           <img :src="scope.row.qualityImg" class="news-image"/>
           </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['productTestInfo:testInfot:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['productTestInfo:testInfot:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改质检管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <div class="add">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <div class="add-box">
          <el-form-item label="产品名称" prop="productName">
            <el-select v-model="form.mainIdentification" placeholder="请选择">
                <el-option
                  v-for="(item, index) in plantProcessInfoList"
                  :label="item.productName"
                  :key="index"
                  :value="item.productName"
                ></el-option>
              </el-select>
          </el-form-item>


        <el-form-item label="检测类型">
           <el-radio v-model="radio" label="快速检测">快速检测</el-radio>
          <el-radio v-model="radio" label="定量检测">定量检测</el-radio>

        </el-form-item>
       
        <el-form-item label="产品追溯码" prop="productTraceCode">
          <el-input v-model="form1.productBatchCode" placeholder="请输入产品追溯码" disabled/>
        </el-form-item>
        
        <el-form-item label="产品分类代码" prop="productClassification">
          <el-input v-model="form.mainIdentification" placeholder="请输入产品分类" disabled/>
        </el-form-item>
        <el-form-item label="质检情况" prop="qualityNspection">
          <el-input v-model="form.qualityNspection" placeholder="请输入质检单位" />
        </el-form-item>
        <el-form-item label="质检单位" prop="qualityCompany">
          <el-input v-model="form.qualityCompany" placeholder="请输入质检单位" />
        </el-form-item>
        
      </div>
        <el-form-item label="质检描述" prop="qualityDesc">
          <el-input v-model="form.qualityDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="质检结论" prop="qualityConclusion">
          <el-input v-model="form.qualityConclusion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
       
        <el-form-item label="质检报告" prop="qualityImg">
          <div class="updateimg">
         <el-upload
          :action="MixinUploadAllApi"
          :on-success="uploadSuccess"
          :on-remove="delGiftImg"
          :file-list="fileList"
          :multiple="false"
          :limit="1"
          :before-upload="beforeImgUpload"
        >
        <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">&nbsp;建议上传jpg/png文件，尺寸（宽*高）750*327</div>
        </el-upload>
        <img :src="photo" /></div>
        </el-form-item>
      </el-form>  
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTestInfot, getTestInfot, delTestInfot, addTestInfot, updateTestInfot, exportTestInfot ,getTree,listHarvestInfo} from "@/api/productTestInfo/testInfot";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "TestInfot",
  components:{Treeselect},
  data() {
    return {
      form1:[],
      // 上传图片后显示的图片路径
      photo:"",
      // 图片上传
       fileList: [],

      // 单选项
      radio: '快速检测',
        // 树状数
      deptOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 下拉菜单选项
      plantProcessInfoList: [],
      MixinUploadAllApi: process.env.VUE_APP_BASE_API + '/file/upload',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 质检管理表格数据
      testInfotList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productCode: undefined,
        testType: undefined,
        mainIdentification: undefined,
        productTraceCode: undefined,
        productClassification: undefined,
        qualityNspection: undefined,
        qualityCompany: undefined,
        qualityDesc: undefined,
        qualityConclusion: undefined,
        qualityImg: undefined,
        qualityTime: undefined
      },
       queryParams1: {
        pageNum: 1,
        pageSize: 1000,
        productCode: undefined,
        testType: undefined,
        mainIdentification: undefined,
        productTraceCode: undefined,
        productClassification: undefined,
        qualityNspection: undefined,
        qualityCompany: undefined,
        qualityDesc: undefined,
        qualityConclusion: undefined,
        qualityImg: undefined,
        qualityTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
   watch:{
     "form.mainIdentification": function () {
       console.log("变化了");
      for (let i = 0; i < this.plantProcessInfoList.length; i++) {
        if (this.plantProcessInfoList[i].productName == this.form.mainIdentification) {
           this.form1.productBatchCode = this.plantProcessInfoList[i].productBatchCode;
           this.form.harvestId=this.plantProcessInfoList[i].id;
           this.form.enterId=this.plantProcessInfoList[i].enterId;
        }
        else{
          this.form1=[];
        }
      }
    },
  },
  created() {
    this.getList();
    this.getTree();
  },
  methods: { 
    // 添加时下拉菜单
     getList11() {
      listHarvestInfo(this.queryParams1).then((response) => {
        this.plantProcessInfoList = response.rows;
      });
    },
    // 图片上传时
     submitUpload() {
        this.$refs.upload.submit();
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
    // 序号
    indexMethod(index) {
      return index + 1;
    },
    // 行政区划分列表
    getTree() {
      this.loading = true;
      getTree().then((response) => {
        this.deptOptions = this.handleTree(response.data, "id");
      });
    },
    // 树形
     normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children,
      };
    },
    /** 查询质检管理列表 */
    getList() {
      this.loading = true;
      listTestInfot(this.queryParams).then(response => {
        this.testInfotList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    uploadSuccess(res) {
       console.log(res);
       this.photo=res.url;
        this.$set(this.form, 'qualityImg', res.url);
        this.$refs['form'].validateField('qualityImg')
      },
    /** 图片移除之后 */
    delGiftImg() {
        this.fileList = []
        this.form.avatar = '';
        this.photo="";
        this.$refs['form'].validateField('qualityImg')
    }, 
    /** 图片上传之前的校验  */
    beforeImgUpload(file) {
        const isLt1M = file.size / 5120 / 5120 < 1
        if (!isLt1M) {
          this.msgWarning('上传图片大小不能超过 5MB!')
        }
        var FileExt = file.name.replace(/.+\./, '')
        if (['jpg', 'jpeg', 'png'].indexOf(FileExt.toLowerCase()) === -1) {
           this.msgWarning('请上传后缀名为jpg/jpeg/png的图片附件！')
          return false
        }
        return isLt1M
      }, 

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        productCode: undefined,
        testType: undefined,
        mainIdentification: undefined,
        productTraceCode: undefined,
        productClassification: undefined,
        qualityNspection: undefined,
        qualityCompany: undefined,
        qualityDesc: undefined,
        qualityConclusion: undefined,
        qualityImg: undefined,
        qualityTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams={
        pageNum: 1,
        pageSize: 10,
        productCode: undefined,
        testType: undefined,
        mainIdentification: undefined,
        productTraceCode: undefined,
        productClassification: undefined,
        qualityNspection: undefined,
        qualityCompany: undefined,
        qualityDesc: undefined,
        qualityConclusion: undefined,
        qualityImg: undefined,
        qualityTime: undefined
      },
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.fileList=[];
      this.photo="";
      this.reset();
      this.open = true;
     this.getList11();
      this.title = "添加质检管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
     this.getList11();
      const id = row.id || this.ids
      getTestInfot(id).then(response => {
        this.form = response.data;
        this.photo=response.data.qualityImg;
        this.open = true;
        this.radio=response.data.testType;
        this.title = "修改质检管理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        this.form.testType=this.radio;
        if (valid) {
          if (this.form.id != undefined) {
            updateTestInfot(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addTestInfot(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除质检管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTestInfot(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有质检管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTestInfot(queryParams);
        }).then(response => {
          this.download(response.data);
        }).catch(function() {});
    }
  }
};
</script>
<style lang="scss" scoped>
 .news-image {
    width: 50px;
    height: 50px;
  }
.shuxing
{
   width: 250px;
  float: left;
   .shuxing1{
     width: 150px !important;
    height: 35px;
   }
}
.add .el-form-item{
  width: 100%;
}
.add{
  width: 100%;
  .el-select{
    width: 100%;
  }
  .el-form .add-box{
    .el-form-item{
  width: 47%;
}
    width: 100%;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    }
}
.updateimg{
  width: 20%;
  max-width: 500px;
  min-width: 200px;
}
.updateimg img{
  width: 150px;
  height: 150px;
}
</style>